package com.itheima.exer.exercise.ex0924;

import java.util.Scanner;

public class Demo5 {
    public static void main(String[] args) {
        int[] arr = {35, 60, 78, 90, 90, 99, 100};
        System.out.println("请输入你要查找的数据:");
        int number = new Scanner(System.in).nextInt();
        int index = getIndex(arr, number);
        if (index == -1){
            System.out.println("您查找的数据不存在!");
        }else {
            System.out.println("您查找的数据在数组的第"+index+"个索引!");
        }
    }

    private static int getIndex(int[] arr, int number) {
        if (number < arr[0] || number > arr[arr.length - 1]) {
            return -1;
        }
        int max = arr.length - 1;
        int min = 0;
        while (min <= max) {
            int mid = (min + max) / 2;
            if (number == arr[mid]) {
                return mid;
            } else if (number < arr[mid]) {
                max = mid - 1;
            } else if (number > arr[mid]) {
                min = mid + 1;
            }
        }
        return -1;
    }
}
